package Controlador;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Conexion {

    private String IP;
    private String Port;
    private String DB;
    private String User;
    private String Password;
    private String Driver;
    private String URL;
    private Connection Conexion;
    private Statement Buffer;
    private boolean Activo;

    public Conexion(String IP, String Port){
        this.IP = IP;
        this.Port = Port;
        this.Activo = true;
        this.Driver = "oracle.jdbc.driver.OracleDriver";
//        this.User = "BARIAS";
//        this.Password = "ADBD2011";
        this.User = "siad";
        this.Password = "siad";
//        TestUserPasswd();
        this.DB = "XE";
        this.URL = "jdbc:oracle:thin:@" + this.IP + ":" + this.Port + ":" + this.DB;
        try {
            Class.forName(this.Driver);
            try {
                this.Conexion = DriverManager.getConnection(this.URL, this.User, this.Password);
                this.Conexion.setAutoCommit(false);
                try {
                    this.Buffer = this.Conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                } catch (Exception ex){
                    this.Activo = false;
                    System.out.println("Error al crear el Statement");
                }
            } catch (Exception ex){
                this.Activo = false;
                System.out.println("Error en conexion");
            }
        } catch (Exception ex){
            this.Activo = false;
            System.out.println("Driver No encontrado");
        }
    }
     public Conexion(){
        this.IP = "127.0.0.1";
        this.Port = "1521";
        this.Activo = true;
        this.Driver = "oracle.jdbc.driver.OracleDriver";
        this.User = "siad";
        this.Password = "siad";
//        TestUserPasswd();
        this.DB = "XE";
        this.URL = "jdbc:oracle:thin:@" + this.IP + ":" + this.Port + ":" + this.DB;
        try {
            Class.forName(this.Driver);
            try {
                this.Conexion = DriverManager.getConnection(this.URL, this.User, this.Password);
                this.Conexion.setAutoCommit(false);
                try {
                    this.Buffer = this.Conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                } catch (Exception ex){
                    this.Activo = false;
                    System.out.println("Error al crear el Statement");
                }
            } catch (Exception ex){
                this.Activo = false;
                System.out.println("Error en conexion");
            }
        } catch (Exception ex){
            this.Activo = false;
            System.out.println("Driver No encontrado");
        }
    }
     
    public void Commit(boolean commit){
        try {
            if(commit)
            this.Conexion.commit();
        else
            this.Conexion.rollback();
        } catch (Exception e) {
            System.out.println("Error: "+e.getMessage());
        }
        
    }
//    private void TestUserPasswd(){
//        Document Documento;
//        try{
//            Documento = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("Siad/SIAD.xml"));
//            this.User = Documento.getElementsByTagName("user").item(0).getTextContent();
//            this.Password = Documento.getElementsByTagName("passwd").item(0).getTextContent();
//        }
//        catch(Exception e){
//            System.out.println("Error: "+e.getMessage());
//        }
//    }
    public ResultSet getQuery(String Query){
        ResultSet Result = null;
        try{
            Result = Buffer.executeQuery(Query);
        }
        catch(Exception ex){System.out.println("Error en la consulta: " + Query);}
        return(Result);
    }
    
    public int getUpdate(String Update){
        int Result = 0;
        try{
            Result = Buffer.executeUpdate(Update);
        }
        catch(Exception ex){Result = -1; System.out.println("Error en la Actualización: " + Update);}
        return(Result);
    }
    
    public void Close(){
        try{
            this.Buffer.close();
            this.Conexion.close();
        }
        catch(Exception ex){System.out.println("Error al cerrar conexion");}
    }
    
    public int getRowCount(ResultSet resultado){
        int retorno =0;
        try{
        resultado.last();
        retorno = resultado.getRow();
        resultado.beforeFirst();
        }
        catch(Exception e){
            retorno =0;
        }
        return retorno;
    }

    public boolean isActivo(){
        return this.Activo;
    }
}